/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.iss.examsys.view;

import com.iss.examsys.ejb.InvigilateExamBean;
import com.iss.examsys.ejb.StudentExamStatusBean;
import com.iss.examsys.entity.ExamSession;
import com.iss.examsys.entity.Module;
import com.iss.examsys.entity.StudentExamStatus;
import com.iss.examsys.entity.StudentModule;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.inject.Inject;
import javax.inject.Named;

/**
 *
 * @author User
 */
@Named
@SessionScoped
public class InvigilateExamListView implements Serializable{
    
    @EJB InvigilateExamBean invigilateExamBean;
    @EJB StudentExamStatusBean studentExamStatusBean;
    @Inject ExamStudentStatusView examStudentView;
    
    private List<ExamSession> examList = new ArrayList<ExamSession>();
    private ExamSession selectedExamSession = new ExamSession();
    private StudentModule selectedStudentModule;
    
    @PostConstruct
    public void init() {
        examList = invigilateExamBean.getListOfExam();
    }
    
    @PreDestroy
    public void reset () {
        selectedExamSession = new ExamSession();
        examList = new ArrayList<ExamSession>();
        studentExamStatusBean.clearStudentExamStatus();
    }

    public ExamSession getSelectedExamSession() {
        return selectedExamSession;
    }

    public void setSelectedExamSession(ExamSession selectedExamSession) {
        this.selectedExamSession = selectedExamSession;
        
        List<StudentModule> studentList = selectedExamSession.getExamPaperId().getModuleId().getStudentModuleList();
        for(Iterator iter = studentList.iterator();iter.hasNext();){
            StudentModule studentModule = (StudentModule) iter.next();
            long examSessionId = selectedExamSession.getId();
            studentExamStatusBean.add(new StudentExamStatus(studentModule.getStudentId().getUserId(),examSessionId,0,"Not Signed In"));
        }
    }
   
    public List<StudentExamStatus> getStudentExamStatus(){
        return studentExamStatusBean.getStudentsExamStatuses();
    } 
    public StudentModule getSelectedStudentModule() {
        return selectedStudentModule;
    }

    public void setSelectedStudentModule(StudentModule selectedStudentModule) {
        this.selectedStudentModule = selectedStudentModule;
    }
    
    
    public List<ExamSession> getExamList() {
        return examList;
    }
    
    public List<StudentModule> getStudentList(){
        List<StudentModule> studetList = new ArrayList<StudentModule>();
        
        if(selectedExamSession.getExamPaperId().getModuleId().getStudentModuleList().size()>0){
            studetList = selectedExamSession.getExamPaperId().getModuleId().getStudentModuleList();
        }
        return studetList;
    }
    
    public void addStatus(){
        System.out.print("Add status "+examStudentView.getStudentStatusList());
        
        for(StudentExamStatus status : examStudentView.getStudentStatusList()){
            for(StudentExamStatus statusUpdate : studentExamStatusBean.getStudentsExamStatuses()){
                
                if(status.getUserId().equalsIgnoreCase(statusUpdate.getUserId()) && 
                      status.getExamId()==statusUpdate.getExamId()){
                    statusUpdate.setStudentStatus(status.getStudentStatus());
                }
            }
        }
        
        //studentExamStatusBean.setStudentsExamStatuses(studentExamStatusUpdateList);
    }
     
}
